<?php

/**
 * This is the model class for table "moja_baza.urzadzenie".
 *
 * The followings are the available columns in table 'moja_baza.urzadzenie':
 * @property integer $idUrzadzenia
 * @property integer $idTypUrzadzenia
 * @property integer $idStanowiska
 *
 * The followings are the available model relations:
 * @property Stanowisko $idStanowiska0
 * @property Typurzadzenia $idTypUrzadzenia0
 */
class Urzadzenie extends CActiveRecord
{
	public $typUrzadzeniaName;
        public $stanowiskoName;
    
        /**
	 * Returns the static model of the specified AR class.
	 * @return Urzadzenie the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'moja_baza.urzadzenie';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idTypUrzadzenia, idStanowiska', 'required'),
			array('idUrzadzenia, idTypUrzadzenia, idStanowiska', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idUrzadzenia, idTypUrzadzenia, idStanowiska, typUrzadzeniaName, stanowiskoName', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'stanowisko' => array(self::BELONGS_TO, 'Stanowisko', 'idStanowiska'),
			'typurzadzenia' => array(self::BELONGS_TO, 'Typurzadzenia', 'idTypUrzadzenia'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idUrzadzenia' => 'ID',
			'typUrzadzeniaName' => 'Typ urządzenia',
			'stanowiskoName' => 'Stanowisko',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idUrzadzenia',$this->idUrzadzenia);
		if (!empty($this->typUrzadzeniaName)) {$criteria->compare('idTypUrzadzenia',$this->getTypUrzadzeniaIdFromName($this->typUrzadzeniaName));}
                if (!empty($this->stanowiskoName)) {$criteria->compare('idStanowiska',$this->getStanowiskoIdFromName($this->stanowiskoName));}
		$criteria->compare('idStanowiska',$this->idStanowiska);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        private function getTypUrzadzeniaIdFromName($var) {
                $tablica_id = array();

                if (!isset($var)) {
                    $var = "";
                }
                $typyurzadzen = Typurzadzenia::model()->findAll("nazwa LIKE :par", array(':par'=>'%'.$var.'%'));
                foreach ($typyurzadzen as $t)
                    $tablica_id[] = $t->idTypUrzadzenia;

                return count($tablica_id) ? $tablica_id : -1;
        }
        
        public function getStanowiskoIdFromName($var) {
                $tablica_id = array();

                if (!isset($var)) {
                    $var = "";
                }
                $stanowiska = Stanowisko::model()->findAll("nazwa LIKE :par", array(':par'=>'%'.$var.'%'));
                foreach ($stanowiska as $s)
                    $tablica_id[] = $s->idStanowiska;

                return count($tablica_id) ? $tablica_id : -1;
        }
}